﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Columns;
using CDMS.Common;
using CDMS.Entities;
using DevExpress.LookAndFeel;
using DevExpress.XtraSplashScreen;
using CDMS.BLL;
using DevExpress.XtraEditors.Controls;
using CDMS.Forms;
using DevExpress.Utils.Win;
using DevExpress.XtraLayout;
using CDMS.Utilities;
using CDMS.Report;

namespace CDMS
{
    public partial class ucContractList : DevExpress.XtraEditors.XtraUserControl
    {
        Contract_BLL Contract_BLL;
        int iD = 0;
        string caption = "";
        public ucContractList(string caption)
        {
            InitializeComponent();
            this.layoutControl1.AllowCustomizationMenu = false;
            this.caption = caption;
            Contract_BLL = new Contract_BLL();


            LookUpEditDataSource.ContractStatus(lupContractStatus);
            LookUpEditDataSource.ContractType(lupContractType);

            AddColumn();

            LoadData();
        }

        private void AddColumn()
        {
            gridView1.Columns.Clear();

            FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "Contract_ID", "", "", 0, false, true);
            FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "Contract_No", "Số hợp đồng", "", 150, true, true);
            FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "Contract_SignDate", "Ngày ký", "dd/MM/yyyy", 90, true, true);
            FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "Customer_Name", "Khách hàng", "", 250, true, true);
            FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "CustomerType_Name", "Loại hợp đồng", "", 110, true, true);
            FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "Contract_Content", "Nội dung", "", 250, true, true);
            FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "Contract_Value", "Trị giá", "#,##0", 90, true, true);
            FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "ContractStatus_Name", "Trạng thái", "", 90, true, true);
        }


        void LoadData()
        {
            //Open Wait Form
            FormGlobals.ShowWaitForm();
            try
            {
                DataTable dt = new DataTable();
                int CustomerID = Convert.ToInt32(searchControlTimKhachHang.Tag);
                DateTime fromDate = Convert.ToDateTime(dteSignFromDate.EditValue);
                DateTime toDate = Convert.ToDateTime(dteSignToDate.EditValue);
                int ContractTypeID = 0;
                if (!string.IsNullOrEmpty(lupContractType.Text))
                    ContractTypeID = Convert.ToInt32(lupContractType.EditValue);
                int ContractStatusID = 0;
                if (!string.IsNullOrEmpty(lupContractStatus.Text))
                    ContractStatusID = Convert.ToInt32(lupContractStatus.EditValue);

                dt = Contract_BLL.Search(CustomerID, fromDate, toDate, ContractTypeID, ContractStatusID);
                gridControl1.DataSource = dt;
                gridControl1.ForceInitialize();
            }
            finally
            {
                //Close Wait Form
                FormGlobals.CloseWaitForm();
            }
        }

        private void barButtonItemAdd_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {

            Forms.frmContractInfo f = new Forms.frmContractInfo(0);

            if (f.ShowDialog() == DialogResult.OK)
            {
                LoadData();
            }
        }

        private void barButtonItemEdit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (gridView1.FocusedRowHandle < 0)
                return;

            iD = (int)gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns[0]);
            Forms.frmContractInfo f = new Forms.frmContractInfo(iD);

            if (f.ShowDialog() == DialogResult.OK)
            {
                LoadData();
            }
        }


        private void barButtonItemDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (gridView1.FocusedRowHandle < 0)
                return;

            if (FormGlobals.Message_QuestionYesNo("Bạn có chắc chắn muốn xóa dòng này?", "Xác nhận xóa") == DialogResult.Yes)
            {

                iD = (int)gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns[0]);

                if (Contract_BLL.Delete(iD))
                    LoadData();
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            LoadData();
        }

        private void btnExportExcel_Click(object sender, EventArgs e)
        {
            FormGlobals.ExportDataToExcel(gridControl1, caption);
        }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            barButtonItemEdit.PerformClick();
        }


        private void searchControlTimKhachHang_ButtonClick(object sender, ButtonPressedEventArgs e)
        {
            if (e.Button.Kind == ButtonPredefines.Search)
            {
                frmCustomerSearch f = new frmCustomerSearch();
                if (f.ShowDialog() == DialogResult.OK)
                {
                    searchControlTimKhachHang.Text = f.Customer.Customer_Name;
                    searchControlTimKhachHang.Tag = f.Customer.Customer_ID;
                    LoadData();
                }
            }
        }

        private void searchControlTimKhachHang_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = true;
        }

        private void lupContractType_EditValueChanged(object sender, EventArgs e)
        {
            LoadData();
        }

        private void lupContractStatus_EditValueChanged(object sender, EventArgs e)
        {
            LoadData();

        }

        private void barButtonItemPrint_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            frmPreviewReport f = new frmPreviewReport(CDMS.Report.EnumReport.DSHopDong);
            f.ShowDialog();
        }





    }
}
